<template>
	<view>
	    <view class="goods-list">
	      <block v-for="(goods, i) in goodsList" :key="i" >
	        <my-goods :goods="goods"></my-goods>
	      </block>
	    </view>
	  </view>
</template>

<script>
	export default {
		onLoad(options){
			this.queryObj.cid = options.cid || ''
			this.queryObj.query = options.query || ''
			this.getGoodsList()
		},
		onPullDownRefresh() {
			this.queryObj.pagenum = 1
			this.goodsList = []
			this.isloading = true 
			this.getGoodsList(()=>{
				uni.stopPullDownRefresh()
			})
		},
		data() {
			return {
				// 请求参数对象
				    queryObj: {
				      // 查询关键词
				      query: '',
				      // 商品分类Id
				      cid: '',
				      // 页码值
				      pagenum: 1,
				      // 每页显示多少条数据
				      pagesize: 10
				    },
					goodsList:[],
					total:0,
					// 默认的空图片
					isloading:true//默认可以加载下一页数据
					    
			};
		},
		methods:{
			// 获取商品列表数据的方法
			  async getGoodsList(cb) {
			    // 发起请求
			    const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
			    if (res.meta.status !== 200) return uni.$showMsg()
			    // 为数据赋值
			    this.goodsList = [...this.goodsList,...res.message.goods]
			    this.total = res.message.total
				this.isloading = true 
				cb && cb()
			  }
		},
		onReachBottom() {
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total)return uni.$showMsg('数据加载完成')
			if(this.isloading === false)return 
			this.isloading = false 
			this.queryObj.pagenum++
			this.getGoodsList()
		}
	}
</script>

<style lang="scss">

</style>
